<?php
class km_barang_model extends MY_Model {
	var $model = 'km_barang';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	barang_id, mdstruktur_id_div, mdstruktur_id_dep, mdstruktur_id_cls, satuan_id, kemasan_id, barang_barcode, barang_kode, barang_nama, barang_nm, barang_ppn, barang_qty1, barang_hjual1, barang_margin1, barang_qty2, barang_hjual2, barang_margin2, barang_qty3, barang_hjual3, barang_margin3, barang_diskon, barang_hpokok, barang_stok_min, barang_stok_max, barang_image, barang_tipe, barang_limit_max, barang_limit_min, barang_keterangan, barang_status_aktif, satuan_id_besar, kemasan_id_besar, div, dep, cls, supp_id, barang_status_level, kms_karton, supplier, path, harga
					FROM	km_barang 
					WHERE	(UPPER(barang_kode) LIKE '%".strtoupper(post('keyword'))."%' OR 
							UPPER(barang_nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'barang_nama' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetJSON_ac($kode)
	{
		$ls_sql	= "	SELECT	kmb.barang_barcode label, kmb.barang_nm ". '"desc"' .", kmb.barang_hjual1 value, kms.supplier_kode, kms.supplier_nama
					FROM	km_barang kmb JOIN km_barang_supplier kmbs ON kmb.barang_id=kmbs.barang_id JOIN km_supplier kms ON kmbs.supplier_id=kms.supplier_id 
					WHERE	(UPPER(kmb.barang_barcode) LIKE '%".strtoupper($kode)."%')
					ORDER BY ".(post('sort')=='' ? 'kmb.barang_nama' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetJSONByVendor($kd_vendor)
	{
		$ls_sql	= "	SELECT	A.barang_id, A.barang_barcode, A.barang_kode, A.barang_nama, D.satuan_nama as satuan_nama_besar, E.kemasan_nama as kemasan_nama_besar, F.satuan_nama, G.kemasan_nama, A.barang_hpokok*E.kemasan_nama as harga_beli, A.barang_hpokok*G.kemasan_nama as harga_beli_pcs
					FROM	km_barang A
							JOIN km_barang_supplier B ON A.barang_id=B.barang_id
							JOIN km_supplier C ON B.supplier_id=C.supplier_id AND C.supplier_kode='$kd_vendor'
							JOIN km_satuan D ON A.satuan_id_besar=D.satuan_id
							JOIN km_kemasan E ON A.kemasan_id_besar=E.kemasan_id
							JOIN km_satuan F ON A.satuan_id=F.satuan_id
							JOIN km_kemasan G ON A.kemasan_id=G.kemasan_id
					WHERE	(UPPER(A.barang_kode) LIKE '%".strtoupper(post('keyword'))."%' OR 
							UPPER(A.barang_nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'A.barang_nama' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert('km_barang', $arr_data);
	}
	
	function Update($arr_data, $id){
		$this->db->where('barang_id', $id);
		$this->db->update('km_barang', $arr_data);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('km_barang', array('barang_kode' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('km_barang', array('barang_kode' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		/*try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM km_barang a join km_mdstruktur b on a.km_mdstruktur=b.mdstruktur_id WHERE a.barang_kode = '".$arr_rule['barang_kode']."'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['barang_kode']."]<br />";
			}else{*/
				$this->db->delete('km_barang', array('barang_kode' => $arr_rule['barang_kode']));
				return '';
			/*}
		} catch(Exception $e) {
			return $e->getMessage();
		}*/
	}

	function getCombo($database, $field1, $field2){
		$ls_sql	=  "SELECT	$field1 value,
							$field2 display
					FROM	ADMIN_PRIMKOKAS.$database ORDER BY $field1, $field2";
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
}
?>